﻿
using Dapper;
using HBase;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace hCommand {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：Component                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2020/9/2 11:28:16                        
   *└────────────────────────────────────────────────┘
   **/
    public class ContinueInEngine<CS, CSD> :GkTransaction where CS : AbsCoreStock where CSD : AbsCoreStockDetail, new() {
        public long paramId;
        public int src;

        protected override TransRes Execute(IDbConnection connection ,IDbTransaction transaction) {
            CoreTaskParam ctp = connection.GetById<CoreTaskParam>(paramId ,transaction);
            List<CoreTaskParam> ctpList = connection.Query<CoreTaskParam>("select * from Core_Task_Param  where task_Id=@taskId" ,ctp ,transaction).ToList();
            foreach( CoreTaskParam p in ctpList ) {
                if( p.status < 4 ) {
                    return new TransRes("状态异常");
                }
            }
            CoreTask ct = connection.GetById<CoreTask>(ctp.taskId ,transaction);
            CS cs = StockUtil.getStock<CS>(connection ,transaction ,ct.stockId);
            long stockId = cs.id;
            if( stockId == 0 ) {
                return new TransRes("stock 为空");
            }
            List<CSD> stockDetailsList = CsdUtil.GetStockDetailList<CSD>(connection ,transaction ,stockId);
            foreach( CSD csd in stockDetailsList ) {
                if( csd.storeStatus == ( int ) StockDetailStatus.待盘库 ) {

                    return new TransRes("待盘库不能继续入库");
                }
            }
            CoreWhLoc loc = SelectLocUtil.selectLoc(connection ,transaction ,stockDetailsList);
            if( loc == null ) {
                return new TransRes("货位获取异常");

            }
            long paramTaskId = TaskPersistenceUtil.moveCoreTaskParam(connection ,transaction ,null ,ct.id ,src ,( int ) loc.id ,0);
            StockUtil.UpdateStock(connection ,transaction ,cs.id ,StockStatus.移动中);
            CoreUtil.UpdataWhLoc(connection ,transaction ,( int ) loc.id ,ct.goodDiff);

            return new TransRes();
        }

    }
}
